package com.manor.main.utils;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.manor.main.R;
import com.manor.main.bean.ShareBean;
import com.sharesdk.onekeyshare.OnekeyShare;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**

 * @version 1.0
 * @date 2019/6/10  14:42
 */
public class ShareUtil {
    private Context mContext;
    private RecyclerView mRecyclerView;
    private BottomSheetDialog dialog;
    public static final String LINK_CLIP = "LINK";
    private String mTitle;
    private String mTitleUrlId;
    private String mContent;
    private String mImgUrl;
    private String NEWS_URL="http://www.xxxxxxx.net/index/index/share_article?id=";

    public ShareUtil(Context mContext, String title, String titleUrlId, String content, String imgUrl) {
        this.mContext = mContext;
        this.mTitle = title;
        this.mTitleUrlId = titleUrlId;
        this.mContent = content;
        this.mImgUrl = imgUrl;
        initDialog();
    }

    public void showShare() {
        if (dialog != null) {
            dialog.show();
        }
    }

    public void dismissShare() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    private void initDialog() {

        dialog = new BottomSheetDialog(mContext);
        View view = View.inflate(mContext, R.layout.dialog_bottom_sheet, null);
        dialog.setContentView(view);
        dialog.setCancelable(false);
        dialog.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        mRecyclerView = (RecyclerView) dialog.findViewById(R.id.recycler_dialog);
        mRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        mRecyclerView.setHasFixedSize(true);
        MyAdapter myAdapter = new MyAdapter(R.layout.item_dialog_sheet, getList());
        mRecyclerView.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (position) {
                    case 1://微信朋友圈
                        onShare(WechatMoments.NAME,mTitle,NEWS_URL+mTitleUrlId,mContent,mImgUrl);
                        break;
                    case 0://微信好友
                        onShare(Wechat.NAME,mTitle,NEWS_URL+mTitleUrlId,mContent,mImgUrl);
                        break;
                    case 3://复制链接
                        //获取剪贴板管理器：
                        ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                        // 创建普通字符型ClipData
                        ClipData mClipData = ClipData.newPlainText("Label", NEWS_URL+mTitleUrlId);
                        // 将ClipData内容放到系统剪贴板里。
                        cm.setPrimaryClip(mClipData);
//                        ToastUtils.showShort("复制成功，赶紧去分享吧");
                        dismissShare();
                        break;
                    case 2://QQ好友
                        onShare(QQ.NAME,mTitle,NEWS_URL+mTitleUrlId,mContent,mImgUrl);
                        break;
                    case 4://QQ空间
                        onShare(QZone.NAME,mTitle,NEWS_URL+mTitleUrlId,mContent,mImgUrl);
                        break;

                }
            }
        });
    }


    private void onShare(String platform,String title,String titleUrl,String content,String imgUrl) {
        final OnekeyShare oks = new OnekeyShare();
        //指定分享的平台，如果为空，还是会调用九宫格的平台列表界面
        if (platform != null) {
            oks.setPlatform(platform);
        }
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(title);
        // titleUrl是标题的网络链接，仅在Linked-in,QQ和QQ空间使用
        oks.setTitleUrl(titleUrl);
        // text是分享文本，所有平台都需要这个字段
        oks.setText(content);
        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
        oks.setImageUrl(imgUrl);
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(titleUrl);

        //启动分享
        try {
            oks.show(mContext);
            dialog.dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private List<ShareBean> getList() {

        List<ShareBean> list = new ArrayList<>();
        list.add(new ShareBean(R.mipmap.ic_sv_share_wx, "微信", Wechat.NAME));
        list.add(new ShareBean(R.mipmap.ic_sv_share_wx_f, "朋友圈", WechatMoments.NAME));
        list.add(new ShareBean(R.mipmap.ic_sv_share_qq, "QQ", QQ.NAME));
//        list.add(new ShareBean(R.mipmap.share_qq, "QQ好友", QQ.NAME));
//        list.add(new ShareBean(R.mipmap.share_q_zone, "QQ空间", QZone.NAME));
        return list;
    }

    private class MyAdapter extends BaseQuickAdapter<ShareBean, BaseViewHolder> {
        public MyAdapter(int layoutResId, @Nullable List data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, ShareBean item) {
            helper.setText(R.id.tv_text, item.getTitle());
            helper.setBackgroundResource(R.id.iv_img, item.getPic());

        }
    }

}
